package weaver.interfaces.zhlx.duss.action.salesmodule.crm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.CrmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2025年5月18日 16:02
 */
public class WeaJxsdlsspbAction extends BaseBean implements Action {
    private static final String RES_CODE_ZERO = "0";

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaJxsdlsspbAction>>>>>requestid=" + requestid);
        try {
            String status = "3";//"归档：传3-审批通过
            RecordSet rs = new RecordSet();
            String message = "调用CRM系统接口返回:";
            CrmUtil crmUtil = new CrmUtil();
            rs.executeQuery("select crmsjid,pssq,sfyqdxy,spbh,sqhzqx,sqhzqxjs,sqjshzsj,pslx," +
                    "yxwyhpsjg,ghjl,bzdxwb,hl,dljxfl,qtfy,dljxzzcyb,dljxzzcrmb,sfxycxqxy,xylx," +
                    "sfyqdxy,yqxyyxq,yqxyyxqjs,zdxysjy " +
                    "from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                String crmsjid = Util.null2String(rs.getString("crmsjid"));//CRM数据ID
                String pssq = Util.null2String(rs.getString("pssq"));//评审申请
                String sfyqdxy = Util.null2String(rs.getString("sfyqdxy"));//是否已经签订协议
                String spbh = Util.null2String(rs.getString("spbh"));//审批编号
                String sqhzqx = Util.null2String(rs.getString("sqhzqx"));//申请合作开始日期
                String sqhzqxjs = Util.null2String(rs.getString("sqhzqxjs"));//申请合作结束日期
                String sqjshzsj = Util.null2String(rs.getString("sqjshzsj"));//申请结束合作时间
                String pslx = Util.null2String(rs.getString("pslx"));//评审类型
                String yxwyhpsjg = Util.null2String(rs.getString("yxwyhpsjg"));//营销委员会评审结果
                String ghjl = Util.null2String(rs.getString("ghjl"));//过会记录
                String bzdxwb = Util.null2String(rs.getString("bzdxwb"));//币种
                String hl = Util.null2String(rs.getString("hl"));//汇率
                String dljxfl = Util.null2String(rs.getString("dljxfl"));//代理/经销费率
                String qtfy = Util.null2String(rs.getString("qtfy"));//代理/经销相关其他费用
                String dljxzzcyb = Util.null2String(rs.getString("dljxzzcyb"));//代理/经销总支出(原币)
                String dljxzzcrmb = Util.null2String(rs.getString("dljxzzcrmb"));//代理/经销总支出(人民币)
                String sfxycxqxy = Util.null2String(rs.getString("sfxycxqxy"));//是否需要重新签订协议
                String xylx = Util.null2String(rs.getString("xylx"));//协议类型
                String yqxyyxq = Util.null2String(rs.getString("yqxyyxq"));//已签协议有效期
                String yqxyyxqjs = Util.null2String(rs.getString("yqxyyxqjs"));//已签协议有效期结束
                String zdxysjy = Util.null2String(rs.getString("zdxysjy"));//自动续约时间（月）

                JSONObject dataJson = new JSONObject();
                JSONObject object_data = new JSONObject();
                object_data.put("dataObjectApiName", "custom_object_example1__c");
                object_data.put("reviewCooperation_status__c", pssq);
                object_data.put("has_agreement_been_signed__c", sfyqdxy);
                object_data.put("ec_status__c", status);
                object_data.put("ec_approval_number__c", spbh);
                sqhzqx = !"".equals(sqhzqx) ? sqhzqx + " 00:00:00" : sqhzqx;
                object_data.put("applicationCooperation_sta__c", crmUtil.dateToStamp(sqhzqx));
                sqhzqxjs = !"".equals(sqhzqxjs) ? sqhzqxjs + " 00:00:00" : sqhzqxjs;
                object_data.put("application_cooperation_en__c", crmUtil.dateToStamp(sqhzqxjs));
                sqjshzsj = !"".equals(sqjshzsj) ? sqjshzsj + " 00:00:00" : sqjshzsj;
                object_data.put("application_end_time__c", crmUtil.dateToStamp(sqjshzsj));
                object_data.put("review_type__c", pslx);
                object_data.put("committee_review_result__c", yxwyhpsjg);
                JSONArray ghjlList = crmUtil.getImagefile(ghjl);
                object_data.put("meeting_record__c", ghjlList);
                object_data.put("currency__c", bzdxwb);
                object_data.put("exchange_rate__c", hl);
                object_data.put("agent_fee_rate__c", dljxfl);
                object_data.put("other_fees__c", qtfy);
                object_data.put("total_expense_originalcurrency__c", dljxzzcyb);
                object_data.put("total_expense_cnycurrency__c", dljxzzcrmb);
                object_data.put("is_resign_contract__c", sfxycxqxy);
                object_data.put("agreement_type__c", xylx);
//                yqxyyxq = !"".equals(yqxyyxq) ? yqxyyxq + " 00:00:00" : yqxyyxq;
//                object_data.put("agreement_start_time__c", crmUtil.dateToStamp(yqxyyxq));
//                yqxyyxqjs = !"".equals(yqxyyxqjs) ? yqxyyxqjs + " 00:00:00" : yqxyyxqjs;
//                object_data.put("agreement_end_time__c", crmUtil.dateToStamp(yqxyyxqjs));
//                object_data.put("field_9ep4i__c", zdxysjy);

                object_data.put("_id", crmsjid);
                dataJson.put("skipDataStatusValidate", true);
                dataJson.put("object_data", object_data);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaJxsdlsspbAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaJxsdlsspbAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = crmUtil.excuteHandler(dataJson, dataMap, "WeaJxsdlsspbAction");
                writeLog(">>>>>WeaJxsdlsspbAction>>>>>resultJson=" + resultJson);
                String resultMessage = Util.null2String(resultJson.getString("message"));
                if (!"success".equals(resultMessage)) {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                } else {
                    JSONObject resultJsonObj = resultJson.getJSONObject("data");
                    String errorCode = resultJsonObj.getString("errorCode");
                    if (!RES_CODE_ZERO.equals(errorCode)) {
                        message += Util.null2String(resultJsonObj.getString("errorMessage"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaJxsdlsspbAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
